package com.ajing.logextractor.config;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;

import java.io.File;

public class LogbackConfig {

    public static void initLogbackConfig() {
        LoggerContext loggerContext = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
        String logbackConfigPath = System.getProperty("logback.config.path");
        if (logbackConfigPath == null) {
            return;
        }
        File file = new File(logbackConfigPath);
        if (file.exists() && file.isFile() && file.canRead()) {
            JoranConfigurator joranConfigurator = new JoranConfigurator();
            joranConfigurator.setContext(loggerContext);
            loggerContext.reset();
            try {
                joranConfigurator.doConfigure(file);
            } catch (JoranException e) {
                // 使用默认配置文件
            }
        }
    }

}
